package cn.lds.j375.chlm.serivce.impl;

import cn.lds.j375.chlm.dto.ResultDTO;
import cn.lds.j375.chlm.entity.Menu;
import cn.lds.j375.chlm.entity.Order;
import cn.lds.j375.chlm.entity.OrderDetails;
import cn.lds.j375.chlm.entity.User;
import cn.lds.j375.chlm.exception.ChlmException;
import cn.lds.j375.chlm.mapper.MenuMapper;
import cn.lds.j375.chlm.mapper.OrderDetailsMapper;
import cn.lds.j375.chlm.mapper.OrderMapper;
import cn.lds.j375.chlm.mapper.UserMapper;

import cn.lds.j375.chlm.serivce.OrderService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

@Service
public class OrderServiceImpl implements OrderService {


    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private  MenuMapper menuMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private  OrderDetailsMapper orderDetailsMapper;



    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public ResultDTO<List<Order>> selectOrderByUserId(Integer userId) {

        List<Order> orders = orderMapper.selectOrderByUserId(userId);

        if (null == orders || orders.size() ==0){
            return ResultDTO.buildError("您还没有下过单，是不是没有钱？");
        }


        return ResultDTO.buildSuccess(orders);
    }

    @Override
    public List<OrderDetails> establistOrderDetails(Integer[] mids, Integer[] ns) {

        List<OrderDetails> orderDetails = new ArrayList<>();

        for (int i = 0; i < mids.length; i++) {

            OrderDetails od = new OrderDetails();

            od.setMenuId(mids[i]);

            od.setCount(ns[i]);

            Menu menu = menuMapper.selectMenuById(od.getMenuId());

            od.setMenu(menu);

            od.setOrderDetailPrice(menu.getPrice().multiply(new BigDecimal(od.getCount())));

            orderDetails.add(od);
        }

        return orderDetails;

    }

    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    @Override
    public ResultDTO addOrder(Integer userId, List<OrderDetails> orderDetails) throws Exception {


        //定义一个变量，保存总价格
        BigDecimal totalPrice = new BigDecimal("0");

        //计算总价
        for (OrderDetails orderDetail : orderDetails) {
            totalPrice = totalPrice.add(orderDetail.getOrderDetailPrice());
        }


        //判断用户是否又足够的钱支付
        User user = userMapper.selectUserById(userId);

        if (user.getMoney().compareTo(totalPrice) < 0) {
            throw new ChlmException("您的钱不够，请充值后再来");
        }
        //扣除用户的钱
        user.setMoney(user.getMoney().subtract(totalPrice));

        userMapper.modifyUser(user);

//            int i = 1/0;

        //封装总订单数据
        Order order = new Order();

        order.setUserId(userId);
        order.setTotalPrice(totalPrice);

        //将订单存入数据库
        orderMapper.addOrder(order);


        //获取订单id
        Integer orderId = order.getId();

        //封装订单详情，存入数据库
        for (OrderDetails orderDetail : orderDetails) {
            orderDetail.setOrderId(orderId);

            orderDetailsMapper.addOrderDetails(orderDetail);
        }


        return ResultDTO.buildSuccess();
    }
}
